﻿using UnityEngine;
using System.Collections;

public class ApplicationBtn : MonoBehaviour {

	public bool bSwipeToTop;
	bool bSwipeOnce;

	public bool bHireThis;
    bool bPopOut;
    float fPopSpeed;

	public int CurAppNum = 0;
	GameObject RightWdwMgr;
	GameObject Perk;

	TweenPosition[] TweenerPos;
	Vector3 FirstPos;
	public bool Selected = false;
	public UnitData m_unit;

    public PAPER_TYPE paperType;

	void Start(){
        fPopSpeed = 50.0f * Time.fixedDeltaTime;
        bPopOut = false;

		switch(m_unit.ClassStatus.enClass){
		case CLASS_TYPE.TECHNICIAN:
			GetComponent<UISprite>().spriteName = "Application_Tech";
            transform.FindChild("NameText").transform.localPosition = new Vector3(5.5f, 5);
            transform.GetComponent<UISprite>().color= new Color(215/255f, 200/255f, 200/255f);
            paperType = PAPER_TYPE.APPLICATION_TECH;
			break;

		case CLASS_TYPE.POLICE:
            GetComponent<UISprite>().spriteName = "Application";
            transform.FindChild("NameText").transform.localPosition = new Vector3(-0.5f, 16);
            paperType = PAPER_TYPE.APPLICATION_POLICE;
			break;
		}

		bHireThis = false;

		bSwipeToTop = false;
		bSwipeOnce = false;

		transform.FindChild ("Face").GetComponent<UISprite> ().spriteName = m_unit.strFaceName;

		RightWdwMgr = GameObject.Find ("RightWdw").gameObject;
		Perk = Resources.Load("Prefaps/UI/StaticUI/Perk/Perk") as GameObject;

		switch(CurAppNum)
		{
		case 0:
			gameObject.transform.localPosition = new Vector3 (-20.5f, -50.0f);
			break;

		case 1:
			gameObject.transform.localPosition = new Vector3 (-11.0f, -50.0f);
			break;

		case 2:
			gameObject.transform.localPosition = new Vector3 (-1.5f, -50.0f);
			break;

		case 3:
			gameObject.transform.localPosition = new Vector3 (8.0f, -50.0f);
			break;

		case 4:
			gameObject.transform.localPosition = new Vector3 (17.5f, -50.0f);
			break;

		case 5:
			gameObject.transform.localPosition = new Vector3 (27.0f, -50.0f);
			break;
		}

		gameObject.transform.localScale = new Vector3 (1.0f, 1.0f);
		FirstPos = gameObject.transform.localPosition;

		TweenerPos = gameObject.GetComponents<TweenPosition> ();
		TweenerPos [0].from.x = gameObject.transform.localPosition.x;
		TweenerPos [0].to.x = gameObject.transform.localPosition.x;
		TweenerPos [1].from.x = gameObject.transform.localPosition.x;


		for (int i = 0; i < m_unit.ListPerk.Count; ++i) {
			(Instantiate (Perk) as GameObject).transform.parent = transform;

            if(paperType == PAPER_TYPE.APPLICATION_POLICE)
                transform.GetChild(i + 3).GetComponent<PerkBtn>().SetInit(m_unit.ListPerk[i], i, PAPER_TYPE.APPLICATION_POLICE);
            else
                transform.GetChild(i + 3).GetComponent<PerkBtn>().SetInit(m_unit.ListPerk[i], i, PAPER_TYPE.APPLICATION_TECH);
		}

        if (m_unit.ListPerk.Count < 2)
        { // empty Perk
			(Instantiate (Perk) as GameObject).transform.parent = transform;

            if (paperType == PAPER_TYPE.APPLICATION_POLICE)
                transform.GetChild(4).GetComponent<PerkBtn>().SetEmpty(1, PAPER_TYPE.APPLICATION_POLICE);
            else
                transform.GetChild(4).GetComponent<PerkBtn>().SetEmpty(1, PAPER_TYPE.APPLICATION_TECH);
		}

		
		gameObject.GetComponent<UISprite> ().depth = gameObject.transform.parent.gameObject.GetComponent<UISprite> ().depth + ((CurAppNum+1) * 3);
		transform.FindChild ("Face").GetComponent<UISprite> ().depth = GetComponent<UISprite> ().depth + 1;

        transform.FindChild("NameText").GetComponent<UILabel>().text = string.Format("{0} {1}", m_unit.strFirstName, m_unit.strLastName);
        transform.FindChild("NameText").GetComponent<UILabel>().depth = transform.GetComponent<UISprite>().depth + 1;
	}

	void OnHover(bool isOver){
        if (!Selected)
        {
            if (isOver)
            {
                GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/PageTurn_0"));
                bPopOut = true;
            }
            else
            {
                bPopOut = false;
            }
        }
	}

	void OnClick(){

		if (!GameObject.Find ("RightWdw").GetComponent<RightWdwMgr> ().bSwipeToTop) {
			if (!Selected) {
				TweenerPos [1].Play (true);
				Selected = true;
                bPopOut = false;
				RightWdwMgr.GetComponent<RightWdwMgr> ().CurApplicationNum = CurAppNum;
			} else {
				BackToPositionApp ();
				RightWdwMgr.GetComponent<RightWdwMgr> ().CurApplicationNum = 99;
			}
		}
	}

	void BackToPositionApp(){
		TweenerPos [1].from.y = -50.0f;
		TweenerPos [1].Play (false);
		EventDelegate.Add(TweenerPos [1].onFinished, SelectedFalse);
	}

	void SelectedFalse(){
		TweenerPos [1].from.y = -45.0f;
		TweenerPos [1].RemoveOnFinished (new EventDelegate (SelectedFalse));
	}

	void Update(){

		if (bSwipeToTop && !bSwipeOnce) {
			TweenerPos [2].Play (true);
			HireThis();

			bSwipeOnce = true;
		} else if(!bSwipeToTop) {

			if (Selected) {
				if (RightWdwMgr.GetComponent<RightWdwMgr> ().CurApplicationNum != CurAppNum) {
					BackToPositionApp ();
				}
				if (FirstPos == gameObject.transform.localPosition) {
					Selected = false;
				}
			}
		}
        PopOutApp();
	}

	public void DestroyApp(){
		Destroy (gameObject);
	}

	void HireThis(){
		bHireThis = true;
	}

    void PopOutApp()
    {
        if (!Selected)
        {
            if (bPopOut)
            {
                if (transform.localPosition.y < -45.0f)
                    transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + fPopSpeed);
                else
                    transform.localPosition = new Vector3(transform.localPosition.x, -45.0f);
            }
            else
            {
                if (transform.localPosition.y > -50.0f)
                    transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y - fPopSpeed);
                else
                    transform.localPosition = new Vector3(transform.localPosition.x, -50.0f);
            }
        }
    }
}
